/* eslint-disable jsdoc/require-param */
'use strict';

const Service = require('egg').Service;


class RecordService extends Service {
  /**
   * 查询默认所有
   */
  async findDefault({
    page,
    per_page,
    sortby,
    order,}) {

    const { ctx } = this;

    const param = {
      offset: (parseInt(page) - 1) * parseInt(per_page),
      limit: parseInt(per_page),
      order: [[ sortby, order ]],
      where: {
        is_show: true,
      },
    };

    const result = await ctx.model.Record.findAll(param)
      .then(data => {
        return { success: true, data };
      }).catch(e => {
        return { success: false, data: e };
      });

    return result;
  }

  /**
   * 查询火车,飞机，轮船，大巴
   */
  async findDefaultTransport({
    page,
    per_page,
    sortby,
    order,
    type,
    licence,
    licence_time,
  }) {

    const { ctx } = this;

    const param = {
      offset: (parseInt(page) - 1) * parseInt(per_page),
      limit: parseInt(per_page),
      order: [[ sortby, order ]],
      where: {
        type,
        is_show: true,
      },
    };

    if (licence) {
      param.where.licence = licence;
    }

    if (licence_time) {
      param.where.licence_time = licence_time;
    }

    const result = await ctx.model.Record.findAll(param)
      .then(data => {
        return { success: true, data };
      }).catch(e => {
        return { success: false, data: e };
      });

    return result;
  }

  /**
   * 查询聚集区
   */
  async findDefaultCommunity({
    page,
    per_page,
    sortby,
    order,
    type,
    province,
    cities,
    area,
  }) {

    const { ctx } = this;

    const param = {
      offset: (parseInt(page) - 1) * parseInt(per_page),
      limit: parseInt(per_page),
      order: [[ sortby, order ]],
      where: {
        type,
        is_show: true,
      },
    };

    if (province) {
      param.where.province = province;
    }

    if (cities) {
      param.where.cities = cities;
    }

    if (area) {
      param.where.area = area;
    }

    const result = await ctx.model.Record.findAll(param)
      .then(data => {
        return { success: true, data };
      }).catch(e => {
        return { success: false, data: e };
      });

    return result;
  }


  // 工具函数，转换type为中文
  formatType(type){
    let result = ''
      switch(type){
        case 'train':
          result = '火车'
          break;
        case 'plane':
          result = '飞机'
          break;
        case 'ship':
          result = '轮船'
          break;
        case 'bus':
          result = '巴士'
          break;
        case 'taxi':
          result = '出租'
          break;
        case 'community':
          result = '聚集区'
          break;
        default:
          result = '其他'
      } 
      return result;
  }

  
}

module.exports = RecordService;
